package tptecnicas.logging;

import java.util.ArrayList;
import java.util.List;

import tptecnicas.configuration.LogConfig;
import tptecnicas.printers.ConsoleWriter;
import tptecnicas.printers.CustomWriter;
import tptecnicas.printers.FileTargetWriter;
import tptecnicas.printers.Writer;

/**
 * Clase encargada de la creacion de los escritores especificos segun 
 * la configuracion recibida. 
 * Centraliza la distincion de cada tipo de escritor.
 *
 */
public class WriterFactory {

	public static Writer getWriter(LogConfig config) {
		if (config.getLogPath() == "Terminal") {
			return getConsoleWriter(config);
		}
		else if(config.getLogPath().contains(".xml")) {
			return getCustomWriter(config);
		}
		return getFileWriter(config);
	}

	public static Writer getFileWriter(LogConfig config) {
		List<String> parameters = createList(config.getLogPath(), config.getLogLevel(), config.getLogFormat());
		return new FileTargetWriter(parameters);
	}
	
	public static Writer getFileWriter(String fileTarget) {
		return getFileWriter(new LogConfig(fileTarget, "OFF", "%m"));
	}
	
	public static Writer getConsoleWriter(LogConfig config) {
		List<String> parameters = createList(config.getLogPath(), config.getLogLevel(), config.getLogFormat());
		return new ConsoleWriter(parameters);
	}

	private static List<String> createList(String logPath, String logLevel,
			String logFormat) {
		List<String> list = new ArrayList<>();
		list.add(logPath);
		list.add(logLevel);
		list.add(logFormat);
		return list;		
	}

	public static Writer getConsoleWriter() {
		return getConsoleWriter(new LogConfig("Terminal", "OFF", "%m"));
	}

	public static Writer getCustomWriter(LogConfig config) {
		List<String> parameters = createList(config.getLogPath(), config.getLogLevel(), config.getLogFormat());
		return new CustomWriter(parameters);
	}
}
